package cn.analysis.project.controller;

import cn.analysis.common.ResultVO;
import cn.analysis.common.exception.CustomException;
import cn.analysis.project.domain.CivilCode;
import cn.analysis.project.service.ICivilCodeService;
import cn.analysis.project.service.ISysHolographicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** 全息健康度控制层 */
@RestController
@RequestMapping(value = "holographic")
public class HoloGraphicController {
    public static final Logger log = LoggerFactory.getLogger(HoloGraphicController.class);

    @Autowired
    private ISysHolographicService holographicService;
    @Autowired
    private ICivilCodeService codeService;

    /** 获取所有的全息健康度数据 */
    @RequestMapping(value = "/selectHolographicList", method = RequestMethod.GET)
    public ResultVO selectHolographicList(){
        return ResultVO.success(holographicService.selectList());
    }

    /** 按照传入的参数获取全息健康度数据
     * @param 分类方式
     * */
    @RequestMapping(value = "/selectListByGroup", method = RequestMethod.GET)
    public ResultVO selectListByGroup(String group, String code){
        return ResultVO.success(holographicService.selectListByGroup(group, code));
    }

    /** 查询实时健康度，当前平台的健康度 */
    @RequestMapping(value = "/selectListRealTime", method = RequestMethod.GET)
    public ResultVO selectListRealTime(String code){
        return ResultVO.success(holographicService.selectListRealTime(code));
    }

    /** 通过code查询下级的所有数据 */
    @RequestMapping(value = "/selectHealthByCode", method = RequestMethod.GET)
    public ResultVO selectHealthByCode(String code){
        // 下级的code集合
        List<String> nextCodes = null;
        // 所有下级数据 name&code
        List<CivilCode> civilCodes = null;
        try {
            civilCodes = codeService.selectCivilCodesByCode(code);
            nextCodes = civilCodes.stream().map(item->item.getCode()).collect(Collectors.toList());
        } catch (Exception e) {
            log.error("/holographic/selectHealthByCode: {}", e.getMessage());
            throw new CustomException("/holographic/selectHealthByCode", 500);
        }
//        nextCodes.forEach(System.out::println);
        // 查询出来的数据
        List<Map<String, Object>> returnData = holographicService.selectListByCiviCodes(nextCodes);
        // 将name值设置进去
        List<CivilCode> finalCivilCodes = civilCodes;
        returnData = returnData.stream().map(item -> {
            String code1 = (String)item.get("code");
            for (CivilCode civilCode : finalCivilCodes) {
                if (civilCode.getCode().equals(code1)){
                    item.put("name", civilCode.getName());
                }
            }
//            item.put("name", ""); // 通过item中的code属性从civilCodes中找到一个对象和code和它相等，并返回name
            return item;
        }).collect(Collectors.toList());
        return ResultVO.success(returnData);
    }
}
